<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.contrib.comments.moderation &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../../index.html" />
    <link rel="up" title="django.contrib.comments" href="../comments.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-contrib-comments-moderation">
            
  <h1>Source code for django.contrib.comments.moderation</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">A generic comment-moderation system which allows configuration of</span>
<span class="sd">moderation options on a per-model basis.</span>

<span class="sd">To use, do two things:</span>

<span class="sd">1. Create or import a subclass of ``CommentModerator`` defining the</span>
<span class="sd">   options you want.</span>

<span class="sd">2. Import ``moderator`` from this module and register one or more</span>
<span class="sd">   models, passing the models and the ``CommentModerator`` options</span>
<span class="sd">   class you want to use.</span>


<span class="sd">Example</span>
<span class="sd">-------</span>

<span class="sd">First, we define a simple model class which might represent entries in</span>
<span class="sd">a Weblog::</span>

<span class="sd">    from django.db import models</span>

<span class="sd">    class Entry(models.Model):</span>
<span class="sd">        title = models.CharField(maxlength=250)</span>
<span class="sd">        body = models.TextField()</span>
<span class="sd">        pub_date = models.DateField()</span>
<span class="sd">        enable_comments = models.BooleanField()</span>

<span class="sd">Then we create a ``CommentModerator`` subclass specifying some</span>
<span class="sd">moderation options::</span>

<span class="sd">    from django.contrib.comments.moderation import CommentModerator, moderator</span>

<span class="sd">    class EntryModerator(CommentModerator):</span>
<span class="sd">        email_notification = True</span>
<span class="sd">        enable_field = &#39;enable_comments&#39;</span>

<span class="sd">And finally register it for moderation::</span>

<span class="sd">    moderator.register(Entry, EntryModerator)</span>

<span class="sd">This sample class would apply two moderation steps to each new</span>
<span class="sd">comment submitted on an Entry:</span>

<span class="sd">* If the entry&#39;s ``enable_comments`` field is set to ``False``, the</span>
<span class="sd">  comment will be rejected (immediately deleted).</span>

<span class="sd">* If the comment is successfully posted, an email notification of the</span>
<span class="sd">  comment will be sent to site staff.</span>

<span class="sd">For a full list of built-in moderation options and other</span>
<span class="sd">configurability, see the documentation for the ``CommentModerator``</span>
<span class="sd">class.</span>

<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">datetime</span>

<span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.core.mail</span> <span class="kn">import</span> <span class="n">send_mail</span>
<span class="kn">from</span> <span class="nn">django.contrib.comments</span> <span class="kn">import</span> <span class="n">signals</span>
<span class="kn">from</span> <span class="nn">django.db.models.base</span> <span class="kn">import</span> <span class="n">ModelBase</span>
<span class="kn">from</span> <span class="nn">django.template</span> <span class="kn">import</span> <span class="n">Context</span><span class="p">,</span> <span class="n">loader</span>
<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">comments</span>
<span class="kn">from</span> <span class="nn">django.contrib.sites.shortcuts</span> <span class="kn">import</span> <span class="n">get_current_site</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">timezone</span>

<span class="k">class</span> <span class="nc">AlreadyModerated</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Raised when a model which is already registered for moderation is</span>
<span class="sd">    attempting to be registered again.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">pass</span>

<span class="k">class</span> <span class="nc">NotModerated</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Raised when a model which is not registered for moderation is</span>
<span class="sd">    attempting to be unregistered.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">pass</span>

<div class="viewcode-block" id="CommentModerator"><a class="viewcode-back" href="../../../../ref/contrib/comments/moderation.html#django.contrib.comments.moderation.CommentModerator">[docs]</a><span class="k">class</span> <span class="nc">CommentModerator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Encapsulates comment-moderation options for a given model.</span>

<span class="sd">    This class is not designed to be used directly, since it doesn&#39;t</span>
<span class="sd">    enable any of the available moderation options. Instead, subclass</span>
<span class="sd">    it and override attributes to enable different options::</span>

<span class="sd">    ``auto_close_field``</span>
<span class="sd">        If this is set to the name of a ``DateField`` or</span>
<span class="sd">        ``DateTimeField`` on the model for which comments are</span>
<span class="sd">        being moderated, new comments for objects of that model</span>
<span class="sd">        will be disallowed (immediately deleted) when a certain</span>
<span class="sd">        number of days have passed after the date specified in</span>
<span class="sd">        that field. Must be used in conjunction with</span>
<span class="sd">        ``close_after``, which specifies the number of days past</span>
<span class="sd">        which comments should be disallowed. Default value is</span>
<span class="sd">        ``None``.</span>

<span class="sd">    ``auto_moderate_field``</span>
<span class="sd">        Like ``auto_close_field``, but instead of outright</span>
<span class="sd">        deleting new comments when the requisite number of days</span>
<span class="sd">        have elapsed, it will simply set the ``is_public`` field</span>
<span class="sd">        of new comments to ``False`` before saving them. Must be</span>
<span class="sd">        used in conjunction with ``moderate_after``, which</span>
<span class="sd">        specifies the number of days past which comments should be</span>
<span class="sd">        moderated. Default value is ``None``.</span>

<span class="sd">    ``close_after``</span>
<span class="sd">        If ``auto_close_field`` is used, this must specify the</span>
<span class="sd">        number of days past the value of the field specified by</span>
<span class="sd">        ``auto_close_field`` after which new comments for an</span>
<span class="sd">        object should be disallowed. Default value is ``None``.</span>

<span class="sd">    ``email_notification``</span>
<span class="sd">        If ``True``, any new comment on an object of this model</span>
<span class="sd">        which survives moderation will generate an email to site</span>
<span class="sd">        staff. Default value is ``False``.</span>

<span class="sd">    ``enable_field``</span>
<span class="sd">        If this is set to the name of a ``BooleanField`` on the</span>
<span class="sd">        model for which comments are being moderated, new comments</span>
<span class="sd">        on objects of that model will be disallowed (immediately</span>
<span class="sd">        deleted) whenever the value of that field is ``False`` on</span>
<span class="sd">        the object the comment would be attached to. Default value</span>
<span class="sd">        is ``None``.</span>

<span class="sd">    ``moderate_after``</span>
<span class="sd">        If ``auto_moderate_field`` is used, this must specify the number</span>
<span class="sd">        of days past the value of the field specified by</span>
<span class="sd">        ``auto_moderate_field`` after which new comments for an</span>
<span class="sd">        object should be marked non-public. Default value is</span>
<span class="sd">        ``None``.</span>

<span class="sd">    Most common moderation needs can be covered by changing these</span>
<span class="sd">    attributes, but further customization can be obtained by</span>
<span class="sd">    subclassing and overriding the following methods. Each method will</span>
<span class="sd">    be called with three arguments: ``comment``, which is the comment</span>
<span class="sd">    being submitted, ``content_object``, which is the object the</span>
<span class="sd">    comment will be attached to, and ``request``, which is the</span>
<span class="sd">    ``HttpRequest`` in which the comment is being submitted::</span>

<span class="sd">    ``allow``</span>
<span class="sd">        Should return ``True`` if the comment should be allowed to</span>
<span class="sd">        post on the content object, and ``False`` otherwise (in</span>
<span class="sd">        which case the comment will be immediately deleted).</span>

<span class="sd">    ``email``</span>
<span class="sd">        If email notification of the new comment should be sent to</span>
<span class="sd">        site staff or moderators, this method is responsible for</span>
<span class="sd">        sending the email.</span>

<span class="sd">    ``moderate``</span>
<span class="sd">        Should return ``True`` if the comment should be moderated</span>
<span class="sd">        (in which case its ``is_public`` field will be set to</span>
<span class="sd">        ``False`` before saving), and ``False`` otherwise (in</span>
<span class="sd">        which case the ``is_public`` field will not be changed).</span>

<span class="sd">    Subclasses which want to introspect the model for which comments</span>
<span class="sd">    are being moderated can do so through the attribute ``_model``,</span>
<span class="sd">    which will be the model class.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">auto_close_field</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">auto_moderate_field</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">close_after</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">email_notification</span> <span class="o">=</span> <span class="bp">False</span>
    <span class="n">enable_field</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">moderate_after</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_model</span> <span class="o">=</span> <span class="n">model</span>

    <span class="k">def</span> <span class="nf">_get_delta</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">now</span><span class="p">,</span> <span class="n">then</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Internal helper which will return a ``datetime.timedelta``</span>
<span class="sd">        representing the time between ``now`` and ``then``. Assumes</span>
<span class="sd">        ``now`` is a ``datetime.date`` or ``datetime.datetime`` later</span>
<span class="sd">        than ``then``.</span>

<span class="sd">        If ``now`` and ``then`` are not of the same type due to one of</span>
<span class="sd">        them being a ``datetime.date`` and the other being a</span>
<span class="sd">        ``datetime.datetime``, both will be coerced to</span>
<span class="sd">        ``datetime.date`` before calculating the delta.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">now</span><span class="o">.</span><span class="n">__class__</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">then</span><span class="o">.</span><span class="n">__class__</span><span class="p">:</span>
            <span class="n">now</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="n">now</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">now</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">now</span><span class="o">.</span><span class="n">day</span><span class="p">)</span>
            <span class="n">then</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="n">then</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">then</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">then</span><span class="o">.</span><span class="n">day</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">now</span> <span class="o">&lt;</span> <span class="n">then</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Cannot determine moderation rules because date field is set to a value in the future&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">now</span> <span class="o">-</span> <span class="n">then</span>

<div class="viewcode-block" id="CommentModerator.allow"><a class="viewcode-back" href="../../../../ref/contrib/comments/moderation.html#django.contrib.comments.moderation.CommentModerator.allow">[docs]</a>    <span class="k">def</span> <span class="nf">allow</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comment</span><span class="p">,</span> <span class="n">content_object</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Determine whether a given comment is allowed to be posted on</span>
<span class="sd">        a given object.</span>

<span class="sd">        Return ``True`` if the comment should be allowed, ``False</span>
<span class="sd">        otherwise.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">enable_field</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">content_object</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">enable_field</span><span class="p">):</span>
                <span class="k">return</span> <span class="bp">False</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">auto_close_field</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">close_after</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">close_after_date</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">content_object</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">auto_close_field</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">close_after_date</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_delta</span><span class="p">(</span><span class="n">timezone</span><span class="o">.</span><span class="n">now</span><span class="p">(),</span> <span class="n">close_after_date</span><span class="p">)</span><span class="o">.</span><span class="n">days</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">close_after</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">False</span>
        <span class="k">return</span> <span class="bp">True</span>
</div>
<div class="viewcode-block" id="CommentModerator.moderate"><a class="viewcode-back" href="../../../../ref/contrib/comments/moderation.html#django.contrib.comments.moderation.CommentModerator.moderate">[docs]</a>    <span class="k">def</span> <span class="nf">moderate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comment</span><span class="p">,</span> <span class="n">content_object</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Determine whether a given comment on a given object should be</span>
<span class="sd">        allowed to show up immediately, or should be marked non-public</span>
<span class="sd">        and await approval.</span>

<span class="sd">        Return ``True`` if the comment should be moderated (marked</span>
<span class="sd">        non-public), ``False`` otherwise.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">auto_moderate_field</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">moderate_after</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">moderate_after_date</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">content_object</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">auto_moderate_field</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">moderate_after_date</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_delta</span><span class="p">(</span><span class="n">timezone</span><span class="o">.</span><span class="n">now</span><span class="p">(),</span> <span class="n">moderate_after_date</span><span class="p">)</span><span class="o">.</span><span class="n">days</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">moderate_after</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">True</span>
        <span class="k">return</span> <span class="bp">False</span>
</div>
<div class="viewcode-block" id="CommentModerator.email"><a class="viewcode-back" href="../../../../ref/contrib/comments/moderation.html#django.contrib.comments.moderation.CommentModerator.email">[docs]</a>    <span class="k">def</span> <span class="nf">email</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comment</span><span class="p">,</span> <span class="n">content_object</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Send email notification of a new comment to site staff when email</span>
<span class="sd">        notifications have been requested.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">email_notification</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="n">recipient_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">manager_tuple</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">manager_tuple</span> <span class="ow">in</span> <span class="n">settings</span><span class="o">.</span><span class="n">MANAGERS</span><span class="p">]</span>
        <span class="n">t</span> <span class="o">=</span> <span class="n">loader</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="s">&#39;comments/comment_notification_email.txt&#39;</span><span class="p">)</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><span class="p">({</span><span class="s">&#39;comment&#39;</span><span class="p">:</span> <span class="n">comment</span><span class="p">,</span>
                     <span class="s">&#39;content_object&#39;</span><span class="p">:</span> <span class="n">content_object</span><span class="p">})</span>
        <span class="n">subject</span> <span class="o">=</span> <span class="s">&#39;[</span><span class="si">%s</span><span class="s">] New comment posted on &quot;</span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">get_current_site</span><span class="p">(</span><span class="n">request</span><span class="p">)</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                                                          <span class="n">content_object</span><span class="p">)</span>
        <span class="n">message</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
        <span class="n">send_mail</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEFAULT_FROM_EMAIL</span><span class="p">,</span> <span class="n">recipient_list</span><span class="p">,</span> <span class="n">fail_silently</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</div></div>
<div class="viewcode-block" id="Moderator"><a class="viewcode-back" href="../../../../ref/contrib/comments/moderation.html#django.contrib.comments.moderation.Moderator">[docs]</a><span class="k">class</span> <span class="nc">Moderator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Handles moderation of a set of models.</span>

<span class="sd">    An instance of this class will maintain a list of one or more</span>
<span class="sd">    models registered for comment moderation, and their associated</span>
<span class="sd">    moderation classes, and apply moderation to all incoming comments.</span>

<span class="sd">    To register a model, obtain an instance of ``Moderator`` (this</span>
<span class="sd">    module exports one as ``moderator``), and call its ``register``</span>
<span class="sd">    method, passing the model class and a moderation class (which</span>
<span class="sd">    should be a subclass of ``CommentModerator``). Note that both of</span>
<span class="sd">    these should be the actual classes, not instances of the classes.</span>

<span class="sd">    To cease moderation for a model, call the ``unregister`` method,</span>
<span class="sd">    passing the model class.</span>

<span class="sd">    For convenience, both ``register`` and ``unregister`` can also</span>
<span class="sd">    accept a list of model classes in place of a single model; this</span>
<span class="sd">    allows easier registration of multiple models with the same</span>
<span class="sd">    ``CommentModerator`` class.</span>

<span class="sd">    The actual moderation is applied in two phases: one prior to</span>
<span class="sd">    saving a new comment, and the other immediately after saving. The</span>
<span class="sd">    pre-save moderation may mark a comment as non-public or mark it to</span>
<span class="sd">    be removed; the post-save moderation may delete a comment which</span>
<span class="sd">    was disallowed (there is currently no way to prevent the comment</span>
<span class="sd">    being saved once before removal) and, if the comment is still</span>
<span class="sd">    around, will send any notification emails the comment generated.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>

<div class="viewcode-block" id="Moderator.connect"><a class="viewcode-back" href="../../../../ref/contrib/comments/moderation.html#django.contrib.comments.moderation.Moderator.connect">[docs]</a>    <span class="k">def</span> <span class="nf">connect</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Hook up the moderation methods to pre- and post-save signals</span>
<span class="sd">        from the comment models.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">signals</span><span class="o">.</span><span class="n">comment_will_be_posted</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">pre_save_moderation</span><span class="p">,</span> <span class="n">sender</span><span class="o">=</span><span class="n">comments</span><span class="o">.</span><span class="n">get_model</span><span class="p">())</span>
        <span class="n">signals</span><span class="o">.</span><span class="n">comment_was_posted</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">post_save_moderation</span><span class="p">,</span> <span class="n">sender</span><span class="o">=</span><span class="n">comments</span><span class="o">.</span><span class="n">get_model</span><span class="p">())</span>
</div>
    <span class="k">def</span> <span class="nf">register</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model_or_iterable</span><span class="p">,</span> <span class="n">moderation_class</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Register a model or a list of models for comment moderation,</span>
<span class="sd">        using a particular moderation class.</span>

<span class="sd">        Raise ``AlreadyModerated`` if any of the models are already</span>
<span class="sd">        registered.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">model_or_iterable</span><span class="p">,</span> <span class="n">ModelBase</span><span class="p">):</span>
            <span class="n">model_or_iterable</span> <span class="o">=</span> <span class="p">[</span><span class="n">model_or_iterable</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">model_or_iterable</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">model</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">AlreadyModerated</span><span class="p">(</span><span class="s">&quot;The model &#39;</span><span class="si">%s</span><span class="s">&#39; is already being moderated&quot;</span> <span class="o">%</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span><span class="p">[</span><span class="n">model</span><span class="p">]</span> <span class="o">=</span> <span class="n">moderation_class</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">unregister</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model_or_iterable</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Remove a model or a list of models from the list of models</span>
<span class="sd">        whose comments will be moderated.</span>

<span class="sd">        Raise ``NotModerated`` if any of the models are not currently</span>
<span class="sd">        registered for moderation.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">model_or_iterable</span><span class="p">,</span> <span class="n">ModelBase</span><span class="p">):</span>
            <span class="n">model_or_iterable</span> <span class="o">=</span> <span class="p">[</span><span class="n">model_or_iterable</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">model_or_iterable</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">model</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">NotModerated</span><span class="p">(</span><span class="s">&quot;The model &#39;</span><span class="si">%s</span><span class="s">&#39; is not currently being moderated&quot;</span> <span class="o">%</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span><span class="p">)</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span><span class="p">[</span><span class="n">model</span><span class="p">]</span>

<div class="viewcode-block" id="Moderator.pre_save_moderation"><a class="viewcode-back" href="../../../../ref/contrib/comments/moderation.html#django.contrib.comments.moderation.Moderator.pre_save_moderation">[docs]</a>    <span class="k">def</span> <span class="nf">pre_save_moderation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="n">comment</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Apply any necessary pre-save moderation steps to new</span>
<span class="sd">        comments.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">comment</span><span class="o">.</span><span class="n">content_type</span><span class="o">.</span><span class="n">model_class</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">model</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="n">content_object</span> <span class="o">=</span> <span class="n">comment</span><span class="o">.</span><span class="n">content_object</span>
        <span class="n">moderation_class</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span><span class="p">[</span><span class="n">model</span><span class="p">]</span>

        <span class="c"># Comment will be disallowed outright (HTTP 403 response)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">moderation_class</span><span class="o">.</span><span class="n">allow</span><span class="p">(</span><span class="n">comment</span><span class="p">,</span> <span class="n">content_object</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">False</span>

        <span class="k">if</span> <span class="n">moderation_class</span><span class="o">.</span><span class="n">moderate</span><span class="p">(</span><span class="n">comment</span><span class="p">,</span> <span class="n">content_object</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
            <span class="n">comment</span><span class="o">.</span><span class="n">is_public</span> <span class="o">=</span> <span class="bp">False</span>
</div>
<div class="viewcode-block" id="Moderator.post_save_moderation"><a class="viewcode-back" href="../../../../ref/contrib/comments/moderation.html#django.contrib.comments.moderation.Moderator.post_save_moderation">[docs]</a>    <span class="k">def</span> <span class="nf">post_save_moderation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="n">comment</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Apply any necessary post-save moderation steps to new</span>
<span class="sd">        comments.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">comment</span><span class="o">.</span><span class="n">content_type</span><span class="o">.</span><span class="n">model_class</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">model</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span><span class="p">[</span><span class="n">model</span><span class="p">]</span><span class="o">.</span><span class="n">email</span><span class="p">(</span><span class="n">comment</span><span class="p">,</span> <span class="n">comment</span><span class="o">.</span><span class="n">content_object</span><span class="p">,</span> <span class="n">request</span><span class="p">)</span>

<span class="c"># Import this instance in your own code to use in registering</span>
<span class="c"># your models for moderation.</span></div></div>
<span class="n">moderator</span> <span class="o">=</span> <span class="n">Moderator</span><span class="p">()</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../../index.html">Module code</a>
        
          <ul><li><a href="../../../django.html">django</a>
        
          <ul><li><a href="../comments.html">django.contrib.comments</a>
        
        <ul><li>django.contrib.comments.moderation</li></ul>
        </li></ul></li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>